use std::net::Shutdown::Read;

fn main() {
    println!("Hello, world!");
}


//结构体的定义

#[derive(Debug)]
struct User {
    user_name: String,
    age: i32,
    email: String,
}


#[test]
fn test_struct() {
    //实例化
    let mut user1 = User {
        user_name: String::from("tom"),
        age: 22,
        email: "aaaa@qq.com".to_string(),
    };
    println!("user is {:?}", user1);


    //获取属性
    let user_name = user1.user_name;
    println!("user_name is {}", user_name);

    // 修改属性
    user1.user_name = String::from("9999999");
    let user_name = user1.user_name;
    println!("user_name is {}", user_name);


    //实例化语法的简化,  字段名和变量名一致， 可以简写成下面的形式
    let user_name = String::from("aaaa");
    let age = 33;
    let user2 = User {
        user_name,
        age,
        email: "aaa".to_string(),
    };
    println!("user2 is {:?}", user2);

    //   struct 更新语法
    let user3 = User {
        user_name: String::from("user3"),
        ..user2 //  表示其他属性从 user2 复制过来
    };
    println!("user3 is {:?}", user3);
}


// 空结构体
struct AAA {}


//长方形
#[derive(Debug)]
struct Rectangle {
    width: u32,
    length: u32,
}


#[test]
fn test_struct2() {
    let rectangle = Rectangle {
        width: 20,
        length: 30,
    };

    let area = rectangle.calculcate();

    println!(" rectangle is {:?} , and  area is {}", rectangle, area);


    // 关联函数的调用是   结构体::函数名 
    let aaa = Rectangle::square(11);
    println!("aaa is {:?}", aaa);
}


impl Rectangle {
    //方法 ，第一个参数是 self
    fn calculcate(&self) -> u32 {
        self.length * self.width
    }

    //关联函数,   第一个参数不是self ,  类似java的静态方法
    //通常关联函数用于 构造器  例如String::new() , String::from()

    fn square(size: u32) -> Rectangle {
        Rectangle {
            width: size,
            length: size,
        }
    }
}


